<?php

namespace app\admin\server\music;

use app\admin\model\music\MusicCommentModel;
use app\admin\ServerBase;
use app\logic\single\GoodsSingle;
use think\facade\Db;
use think\Request;

class MusicCommentServer extends ServerBase
{

    function __construct( Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new MusicCommentModel();
    }
    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        [$list,$count] = $this->model->getList($this->request,[],'*',$page,$limit);
        if(!empty($list)){
            foreach ($list as &$val){
                $val['goods'] = GoodsSingle::getInstance()->getGoods($val['goods_id'],$val['type']);
            }
            unset($val);
        }
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }

    function audit(){
        $id = $this->param['id']??0;
        $status = $this->param['audit']??10;
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        if($info['audit'] != 10){
            $this->ERROR='该状态不能审核';
            return false;
        }
        $update['remark'] = $this->param['remark']??'';
        if($status == 20){//审核通过
            $update['audit'] = 20;
        }else{//审核失败
            $update['audit'] = -10;
        }

        Db::startTrans();
        try {
            $info->save($update);
            Db::commit();
            return true;
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR=$exception->getMessage();
            return false;
        }

    }
}